flyui.namespace("flyui.apps.bdgpbygenebatch");

/**
 * @class
 * @constructor
 */
flyui.apps.bdgpbygenebatch.Application = function() {
	
	var that = this;
	
	var batchgenefinder = null;
    var batchbdgp = null;
	
	
	this.initBatchGeneFinder = function() {
	    var context = "flyui.apps.bdgpbygenebatch.Application.initBatchGeneFinder";
	    try {

            flyui.debug("instantiate a service for the batchgenefinder widget", context);
            var service = new flyui.flybase.Service("../../../data/flybase");
            
            flyui.debug("instantiate a renderer for the batchgenefinder widget", context);
            var renderPane = $("#batchGeneFinderWidget");
            var renderer = new flyui.genefinder.BatchWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a genefinder widget", context);
            batchgenefinder = new flyui.genefinder.BatchWidget(service, renderer);
	        
        } catch (error) {
            flyui.apps.bdgpbygenebatch.handleError(error);
        }
	}
		
    this.initBatchBdgp = function() {
        var context = "flyui.apps.bdgpbygenebatch.Application.initBatchBdgp";
        try {

            flyui.debug("instantiate a service for the batchgenefinder widget", context);
            var service = new flyui.bdgp.Service();
            service.setEndpoint("../../../data/bdgp");
            
            flyui.debug("instantiate a renderer for the batchgenefinder widget", context);
            var renderPane = $("#batchBdgpImageWidget");
            var renderer = new flyui.bdgp.BatchWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a bdgp widget", context);
            batchbdgp = new flyui.bdgp.BatchWidget(service, renderer);
            
        } catch (error) {
            flyui.apps.bdgpbygenebatch.handleError(error);
        }
    }
        
	this.init = function() {
        var context = "flyui.apps.bdgpbygenebatch.Application.init";
        try {
    		
    		var logReader = new YAHOO.widget.LogReader("logger");

            this.initBatchGeneFinder();
            this.initBatchBdgp();

            flyui.debug("hook form submission to widget call", context);
            YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
            
            flyui.debug("add listener for genes found event", context);
            batchgenefinder.subscribe("GENESFOUND", this.onGenesFound, null);
            
    		flyui.debug("application loaded, showing main pane", context);
    		$("#loadingPane").hide();
    		$("#applicationPane").show();
    		
        } catch (error) {
            flyui.apps.bdgpbygenebatch.handleError(error);
        }
	};
	
	this.onFormSubmitted = function(event) {
        var context = "flyui.apps.bdgpbygenebatch.Application.onFormSubmitted";
        try {
            flyui.debug("form submitted", context);
            $("#errorMessage")
               .hide()
               .html("this should never be visible");
            $("#batchBdgpImageWidget").hide();
            var query = document.getElementById("queryInput").value;
            flyui.debug("raw query: "+query, context);
//            document.getElementById("queryInput").value = "";

            var names = query.split(" ");
            var cleannames = [];
            for (var i=0; i<names.length; i++) {
                var n = names[i];
                if (n.length > 0) {
                    cleannames[cleannames.length] = n;
                }
            }
            flyui.debug("cleaned names: "+cleannames, context);
            batchgenefinder.findGenesByAnyNameBatch(cleannames);

        } catch (error) {
            flyui.apps.bdgpbygenebatch.handleError(error);
        }
	    
	};
	
    this.onGenesFound = function(type, args) {
        try {
            flyui.debug("application: genes found");
            var querynames = args[0];
            var results = args[1];

            flyui.debug("query names: "+querynames);
            
            var foundnames = [];
            var foundgenes = [];
            
            for (var i=0;i<querynames.length;i++) {
                if (results[querynames[i]].length == 1) {
                    foundnames[foundnames.length] = querynames[i];
                    foundgenes[foundgenes.length] = results[querynames[i]][0];
                }
            }
            
            flyui.debug("found query names: "+foundnames);
            flyui.debug("found genes: "+foundgenes);
            
            batchbdgp.findImagesByGeneBatch(foundnames, foundgenes);
            $("#batchBdgpImageWidget").show();
            
        } catch (error) {
            flyui.apps.bdgpbygene.handleError(error);
        }
    }
	
	
	this.init();
};

flyui.apps.bdgpbygenebatch.handleError = function(error) {
    flyui.err(error.name+" :: "+error.message);
    $("#errorMessage")
       .html("an error has occurred, please see the log for more details")
       .show();
};
    

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new flyui.apps.bdgpbygenebatch.Application();});